﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class Admin_kcgl_kcdd : System.Web.UI.Page
{
    DataClassesDataContext db1 = new DataClassesDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
            string IP = Request.ServerVariables.Get("Remote_Addr").ToString();
            string nowtime = DateTime.Now.ToString();
            DateTime nowtime1 = Convert.ToDateTime(nowtime);
            int nowusername = Convert.ToInt32(Session["username"]);
            var newrizhi = new rizhi
            {
                userName = nowusername,
                time = nowtime1,
                ip = IP,
                caozuo = "进入考场安排管理"
            };
            db1.rizhi.InsertOnSubmit(newrizhi);
            db1.SubmitChanges();
        }
    }
    protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lb1 = (Label)e.Row.Cells[0].FindControl("Label1");
            lb1.Text = (GridView2.PageIndex * GridView2.PageSize + e.Row.RowIndex + 1).ToString();
        }
    }
    protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        // 得到该控件
        GridView theGrid = sender as GridView;
        int newPageIndex = 0;
        if (e.NewPageIndex == -3)
        {
            //点击了Go按钮
            TextBox txtNewPageIndex = null;

            //GridView较DataGrid提供了更多的API，获取分页块可以使用BottomPagerRow 或者TopPagerRow，当然还增加了HeaderRow和FooterRow
            GridViewRow pagerRow = theGrid.BottomPagerRow;

            if (pagerRow != null)
            {
                //得到text控件
                txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;
            }
            if (txtNewPageIndex != null)
            {
                //得到索引
                newPageIndex = int.Parse(txtNewPageIndex.Text) - 1;
            }
        }
        else
        {
            //点击了其他的按钮
            newPageIndex = e.NewPageIndex;
        }
        //防止新索引溢出
        newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
        newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;

        //得到新的值
        theGrid.PageIndex = newPageIndex;
        BindData();
    }
    protected void Button1_Click(object sender, EventArgs e)      //添加考场
    {
        Panel1.Visible = true;
        TextBox3.Text = "30";
    }
    protected void Button4_Click(object sender, EventArgs e)       //保存
    {
        int? i;
        int? num;
        int? flag;
        num = Convert.ToInt32(TextBox3.Text);
        if (CheckBox2.Checked == true)
        {
            i = 1;
        }
        else
        {
            i = 0;
        }
        flag = db1.proc_AddExamPlace(TextBox1.Text, num, TextBox2.Text, i);
        Panel1.Visible = false;
        if (flag == 1)
        {
            Response.Write("<script>alert('考场添加成功！')</script>");
        }
        else if (flag == -1)
        {
            Response.Write("<script>alert('考场更新成功！')</script>");
        }
        else
        {
            Response.Write("<script>alert('考场添加失败！')</script>");
        }
        BindData();
    }
    protected void Button5_Click(object sender, EventArgs e)
    {
        Panel1.Visible = false;
    }
    protected void Button2_Click(object sender, EventArgs e)    //删除考场
    {
        bool jk = false;
        for (int i = 0; i < GridView2.Rows.Count; i++)
        {
            System.Web.UI.WebControls.CheckBox checkBox =
                (System.Web.UI.WebControls.CheckBox)GridView2.Rows[i].FindControl("CheckBox1");
            if (checkBox.Checked == true)
            {
                string num = GridView2.DataKeys[i].Value.ToString();
                db1.proc_DelExamPlace(num);
                jk = true;
            }
        }
        if (jk == true)
        {
            var getE = (from g in db1.proc_GetExamPlace()
                        select g).ToList();
            GridView2.DataSource = getE;
            GridView2.DataBind();
            Response.Write("<script>alert('考场删除成功！')</script>");
        }
        else
        {
            Response.Write("<script>alert('请先选择行！')</script>");
        }
    }

    private void BindData()
    {
        var getE = (from u in db1.proc_GetExamPlace()
                    select new { u.classNo, u.place, u.maxNum, wheClass = u.wheClass > 0 ? "√" : "" }).ToList();
        GridView2.DataSource = getE;
        GridView2.DataBind();
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        string style = @"<style> .text { mso-number-format:\@; } </script> ";
        Response.ClearContent();
        Response.Charset = "GB2312";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
        Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
        Response.ContentType = "application/excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        GridView2.RenderControl(htw);
        Response.Write(style);
        Response.Write(sw.ToString());
        Response.End();
        //导出后恢复分页
        GridView2.AllowPaging = true;
        //重新绑定
        BindData();
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        // Confirms that an HtmlForm control is rendered for
        //为了保险期间还可以在这里加入断条件防止HTML中已经存在该ID
    }
}